"""
Case Type   : tools
Case Name   : 数据库在停止状态，执行pg_resetxlog命令
Create At   : 2021/1/18
Owner       : opentestcase003
Description :
    1.关闭数据库
    2.在对应目录下查看原有xlog列表
    3.执行pg_resetxlog命令
    4.查看执行结果，是否有提示信息
    5.在对应目录下查看现有xlog列表，是否重置成功
Expect      :
    1.检查数据库状态成功
    2.在对应目录下查看xlog列表成功
    3.执行pg_resetxlog命令成功
    4.命令执行成功后的提示信息为：Transaction log reset
    5.对应目录下查看xlog列表成功，日志重置成功
History     :
           modified by opentestcase003 2022/1/28 优化数据库启停步骤
           modified by opentestcase012 2022/11/1 优化用例，build之后添加等待主备同步
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf('Standby' not in Primary_SH.get_db_cluster_status('detail'),
                 '单机环境不执行')
class Tools(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.userNode = Node(node='PrimaryDbUser')
        self.commonsh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
    
    def test_systools(self):
        text = '----step1:关闭数据库;expect:成功----'
        self.logger.info(text)
        Primary_SH.stop_db_cluster()
        text = '---step2:在对应目录下查看原有xlog列表;expect:成功---'
        self.logger.info(text)
        xlog_path = os.path.join(macro.DB_INSTANCE_PATH, 'pg_xlog')
        excute_cmd1 = f'ls -t {xlog_path} | head -1'
        msg2 = self.userNode.sh(excute_cmd1).result()
        self.logger.info(msg2)
        self.assertIn('000', msg2, text + '执行失败')
        
        text = '----step3-4:执行pg_resetxlog命令;expect:成功----'
        self.logger.info(text)
        excute_cmd3 = f'source {macro.DB_ENV_PATH};' \
            f'pg_resetxlog {macro.DB_INSTANCE_PATH}'
        msg3 = self.userNode.sh(excute_cmd3).result()
        self.logger.info(msg3)
        self.assertIn('Transaction log reset', msg3, text + '执行失败')
        
        text = '----step5:在对应目录下查看现有xlog列表是否重置成功;expect:成功----'
        self.logger.info(text)
        excute_cmd5 = f'ls -t {xlog_path} | head -1'
        msg5 = self.userNode.sh(excute_cmd5).result()
        self.logger.info(msg5)
        self.assertIn('000', msg5, text + '执行失败')
        self.assertNotEquals(msg5, msg2, text + '执行失败')
    
    def tearDown(self):
        self.logger.info('-----清理环境-----')
        text1 = '----启动主机,重建备机;expect: 成功----'
        self.logger.info(text1)
        start_res = Primary_SH.start_db_instance(mode="primary")
        build_res = Primary_SH.get_standby_and_build()
        self.logger.info(build_res)

        text2 = '-----校验备机是否完成数据同步;expect: 主备同步-----'
        self.logger.info(text2)
        check_msg = Primary_SH.check_location_consistency(
            'primary', self.commonsh.get_node_num())

        text3 = '-----查看集群状态;expect: 集群状态正常-----'
        self.logger.info(text3)
        rev_msg = Primary_SH.get_db_cluster_status(param='status')
        self.logger.info(rev_msg)

        self.assertIn(self.constant.RESTART_SUCCESS_MSG, start_res,
                      '执行失败:' + text1)
        for msg in build_res:
            self.assertIn(self.constant.BUILD_SUCCESS_MSG,
                          msg, '执行失败:' + text1)
        self.assertTrue(check_msg, '执行失败:' + text2)
        self.assertTrue(rev_msg, '执行失败:' + text3)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
